<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 预约
 */
class subscribe extends CI_Controller {


	public function __construct()
	{
	  	parent::__construct();
	  	$this->load->model(array('subscribe_model'));
	}
	
	/**
	 * [index 预约]
	 * @return [type] [description]
	 */
	public function index()
	{
		$this->load->view('home/communal/subscribe');
	}


	public function subscribe(){
		
		$result = array(
            'err'=>'0',
            'msg'=>''
        );
		$data = $this->input->post();
		// 检查代码合理性
		if(empty($data['mobile']) && empty($_POST['named']) && empty($_POST['demand'])){
			$result['err'] = 1;
            $result['msg'] = '参数不正确,请重新输入';
            exit(json_encode($result));
		}
		$mode='/^1[0-9]{10,10}$/'; // 手机号码匹配
    	if(!preg_match($mode,$data['mobile'],$arr)){
        	$result['err'] = 2;
            $result['msg'] = '手机号码格式不正确,请重新输入!';
            exit(json_encode($result));
    	}
    	$data['status'] = 2 ;
    	$data['date']   = time();
    	if($this->subscribe_model->insert($data)){
    		$mobile = $data['mobile'];
			// 发送短信
    		/*$this->load->library('mobile_sns',array('mobile'=>$mobile));   	
			$reg = $this->mobile_sns->yzm('您好'.$data['named'].'，您预约的产品我们已经收到，企多帮的工作人员会在24小时之内与您取得联系，请耐心等候！');*/
			$reg = 'ok';
			if($reg == 'ok'){
				$result['err'] = 0;
	            exit(json_encode($result));
			}else{
				$result['err'] = 3;
	            $result['msg'] = $this->mobile_sns->error;
	            exit(json_encode($result));
			}

		}else{
			$result['err'] = 4;
            $result['msg'] = '预约失败,系统繁忙请稍后再试!';
            exit(json_encode($result));
		}
	}

}

